import HttpClient from 'http-client'

const postBase = (url, data, callback) => {
    const req = new HttpClient()
    req.post(url, data, res => {
            callback(res)
        },err => {
            console.error('network error:', err)
            let app = getApp()
            let isRelease = app && app.globalData && app.globalData.isReleaseVersion == true
            if(data.isHeartBeat || isRelease) return
            wx.showModal({
                title: err.msg,
                cancelText:'Detail',
                confirmText:'OK',
                showCancel: true,
                content: err.msg,
                success (res) {
                    if (res.cancel) {
                        wx.showModal({
                            title: err.msg,
                            showCancel:false,
                            confirmText:'Done',
                            content: JSON.stringify(err.msgDetail)
                        })
                    }
                }
            })
        }
    )
}
const getBase = (url, data, callback) => {
    const req = new HttpClient()
    req.get(url, data,res => {
            callback(res)
        },err => {
            console.error('network error:', err)
            let app = getApp()
            let isRelease = app && app.globalData && app.globalData.isReleaseVersion == true
            if(data.isHeartBeat || isRelease) return
            wx.showModal({
                title: err.msg,
                cancelText:'Detail',
                confirmText:'OK',
                showCancel: true,
                content: err.msg,
                success (res) {
                    if (res.cancel) {
                        wx.showModal({
                            title: err.msg,
                            showCancel:false,
                            confirmText:'Done',
                            content: JSON.stringify(err.msgDetail)
                        })
                    }
                }
            })
        }
    )
}
const deleteBase = (url, data, callback) => {
    const req = new HttpClient()
    req.delete(url, data,res => {
            callback(res)
        },err => {
            console.error('network error:', err)
            let app = getApp()
            let isRelease = app && app.globalData && app.globalData.isReleaseVersion == true
            if(data.isHeartBeat || isRelease) return
            wx.showModal({
                title: err.msg,
                cancelText:'Detail',
                confirmText:'OK',
                showCancel: true,
                content: err.msg,
                success (res) {
                    if (res.cancel) {
                        wx.showModal({
                            title: err.msg,
                            showCancel:false,
                            confirmText:'Done',
                            content: JSON.stringify(err.msgDetail)
                        })
                    }
                }
            })
        }
    )
}
const patchBase = (url, data, callback) => {
    const req = new HttpClient()
    req.patch(url, data,res => {
            callback(res)
        },err => {
            console.error('network error:', err)
            let app = getApp()
            let isRelease = app && app.globalData && app.globalData.isReleaseVersion == true
            if(data.isHeartBeat || isRelease) return
            wx.showModal({
                title: err.msg,
                cancelText:'Detail',
                confirmText:'OK',
                showCancel: true,
                content: err.msg,
                success (res) {
                    if (res.cancel) {
                        wx.showModal({
                            title: err.msg,
                            showCancel:false,
                            confirmText:'Done',
                            content: JSON.stringify(err.msgDetail)
                        })
                    }
                }
            })
        }
    )
}

/**
 * 首页
 */
const getIndex = (callback, data) => {
  let url = "/kitap"
  getBase(url, data, callback)
}

/**
 * 黄金
 */
const getGold = (callback, data) => {
    let url = "/goldPosts/"
    getBase(url, data, callback)
}

// 黄金产品
const getGoldProduct = (callback, data) => {
    let url = "/goldProduct/"
    getBase(url, data, callback)
}

// 获取黄金店铺
const getGoldShop = (callback, data) => {
    let url = "/goldShop"
    getBase(url, data, callback)
}

// 按 ID 获取黄金店铺详细
const getIdGlodShop = (callback, data) => {
    let url = "/goldShop/"
    if ("shop_id" in data) url += data.shop_id;
    getBase(url, data, callback)
}

// 按 ID 获取产品
const getIdGlodProduct = (callback, data) => {
    let url = "/goldProduct/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 油价
 */
const getOil = (callback, data) => {
    let url = "/oil/"
    getBase(url, data, callback)
}


/**
 * 房屋
 */
// 获取内容
const gethouse = (callback, data) => {
    let url = "/house/"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getHouseDetail = (callback, data) => {
    let url = "/house/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

// 创建内容
const createHouse = (callback, data) => {
    let url = "/house"
    postBase(url, data, callback)
}

/**
 * 工作
 */
// 获取内容
const getJobs = (callback, data) => {
    let url = "/jobs"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getJobsDetail = (callback, data) => {
    let url = "/jobs/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 汽车
 */
// 获取内容
const getCar = (callback, data) => {
    let url = "/car"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getCarDetail = (callback, data) => {
    let url = "/car/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 二手货
 */
// 获取内容
const getSecondHand = (callback, data) => {
    let url = "/secondHand"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getSecondHandDetail = (callback, data) => {
    let url = "/secondHand/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 捡到物品
 */
// 获取内容
const getPickUp = (callback, data) => {
    let url = "/pickUp"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getPickUpDetail = (callback, data) => {
    let url = "/PickUp/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 丢失品
 */
// 获取内容
const getThrow = (callback, data) => {
    let url = "/throw"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getThrowDetail = (callback, data) => {
    let url = "/throw/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 家教
 */
// 获取内容
const getTutor = (callback, data) => {
    let url = "/tutor"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getTutorDetail = (callback, data) => {
    let url = "/tutor/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 手艺
 */
// 获取内容
const getRepairShop = (callback, data) => {
    let url = "/repairShop"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getRepairShopDetail = (callback, data) => {
    let url = "/repairShop/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 代送
 */
// 获取内容
const getDelivery = (callback, data) => {
    let url = "/delivery"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getDeliveryDetail = (callback, data) => {
    let url = "/delivery/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 服务
 */
// 获取内容
const getService = (callback, data) => {
    let url = "/service"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getServiceDetail = (callback, data) => {
    let url = "/service/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 市价
 */
// 获取内容
const getMarketPrice = (callback, data) => {
    let url = "/marketPrice"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getMarketPriceDetail = (callback, data) => {
    let url = "/marketPrice/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 店铺
 */
// 货店铺
const getShop = (callback, goodsData) => {
    let url = "/shop"
    getBase(url, goodsData, callback)
}

// 获取店铺产品
const getGoods = (callback, data) => {
    let url = "/goods/"
    if ("shop_id" in data) url += data.shop_id;
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getShopDetail = (callback, data) => {
    let url = "/shop/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 信息圈
 */
// 获取内容
const getInfoField = (callback, data) => {
    let url = "/infoField"
    getBase(url, data, callback)
}

// 按 ID 获取内容
const getInfoFieldDetail = (callback, data) => {
    let url = "/infoField/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 登录
 */
const loginApi = (callback, data) => {
    let url = "/userLogin"
    postBase(url, data, callback)
}

/**
 * 广告
 */
// 获取
const getAdvertise = (callback, data) => {
    let url = "/advertise/"
    if ("type" in data) url += data.type;
    getBase(url, data, callback)
}

// 按 ID 获取
const getIdAdvertise = (callback, data) => {
    let url = "/advertise/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 阿图什信息圈
 */
const getCorner = (callback, data) => {
    let url = "/corner"
    getBase(url, data, callback)
}

/**
 * 轮播图
 */
// 获取内容
const swiper = (callback, data) => {
    let url = "/getswiper"
    getBase(url, data, callback)
}

// 按 ID 获取
const getIdSwiper = (callback, data) => {
    let url = "/swiper/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 通知
 */
// 获取通知
const getNotification = (callback, data) => {
    let url = "/notification"
    getBase(url, data, callback)
}

// 按 ID 获取
const getNotificationDetailed = (callback, data) => {
    let url = "/notification/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

/**
 * 视频
 */
// 获取视频
const getHomeVideo = (callback, data) => {
    let url = "/getVideo"
    getBase(url, data, callback)
}

// 按 ID 获取视频
const getVideoById = (callback, data) => {
    let url = "/getVideoById/"
    if ("userId" in data) url += data.userId;
    getBase(url, data, callback)
}

/**
 * 景区
 */
// 获取景区
const getScenicSpot = (callback, data) => {
    let url = "/scenicSpot"
    getBase(url, data, callback)
}

// 按 ID 获取
const getScenicSpotDetailed = (callback, data) => {
    let url = "/scenicSpot/"
    if ("postId" in data) url += data.postId;
    getBase(url, data, callback)
}

export default {
    getIndex: getIndex,
    getGold: getGold,
    getGoldProduct: getGoldProduct,
    getGoldShop: getGoldShop,
    getIdGlodShop: getIdGlodShop,
    getIdGlodProduct: getIdGlodProduct,
    getOil: getOil,
    gethouse: gethouse,
    getHouseDetail: getHouseDetail,
    createHouse: createHouse,
    getJobs: getJobs,
    getJobsDetail: getJobsDetail,
    getCar: getCar,
    getCarDetail: getCarDetail,
    getSecondHand: getSecondHand,
    getSecondHandDetail: getSecondHandDetail,
    getPickUp: getPickUp,
    getPickUpDetail: getPickUpDetail,
    getThrow: getThrow,
    getThrowDetail: getThrowDetail,
    getTutor: getTutor,
    getTutorDetail: getTutorDetail,
    getRepairShop: getRepairShop,
    getRepairShopDetail: getRepairShopDetail,
    getService: getService,
    getServiceDetail: getServiceDetail,
    getMarketPrice: getMarketPrice,
    getMarketPriceDetail: getMarketPriceDetail,
    getShop: getShop,
    getGoods: getGoods,
    getShopDetail: getShopDetail,
    loginApi: loginApi,
    getDelivery: getDelivery,
    getDeliveryDetail: getDeliveryDetail,
    getInfoField: getInfoField,
    getInfoFieldDetail: getInfoFieldDetail,
    getAdvertise: getAdvertise,
    getIdAdvertise: getIdAdvertise,
    getCorner: getCorner,
    swiper: swiper,
    getIdSwiper: getIdSwiper,
    getNotification,
    getNotificationDetailed,
    getHomeVideo,
    getVideoById,
    getScenicSpot,
    getScenicSpotDetailed
}